//var service = angular.module('starter.services', []);
service.factory('missionFactory',['$q',function($q){

    var Mission = AV.Object.extend('Mission');
    var CourierCart = AV.Object.extend('CourierCart');
    var Product = AV.Object.extend('Product');
    var Order = AV.Object.extend('Order');
    return {
        PAGE_SIZE:3,
        getMissionInfo:function(page){
            var storage = window.localStorage;
            var uid = storage.getItem("uid");
            var queryMission = new AV.Query(Mission);
            queryMission.skip(this.PAGE_SIZE * (page - 1));
            queryMission.equalTo('uid',uid+'');
            queryMission.equalTo('status',1);
            queryMission.descending('updatedAt');
            queryMission.limit(this.PAGE_SIZE);
            return queryMission.find();
        },
        getMissionCount:function(){
            var storage = window.localStorage;
            var uid = storage.getItem("uid");
            var queryMission = new AV.Query(Mission);
            queryMission.equalTo('uid',uid+'');
            queryMission.equalTo('status',1);
            return queryMission.count();
        },
        updateMissionInfo:function(orderid,info){
            var q = $q.defer();
            var storage = window.localStorage;
            var uid = storage.getItem("uid");
            var queryMission = new AV.Query(Mission);
            queryMission.equalTo('uid',uid+'');
            queryMission.equalTo('orderid',orderid+'');
            queryMission.equalTo('status',1);
            queryMission.first().then(function(res){
                if(res){
                    res.save(info).then(function(){
                        q.resolve({code:0});
                    }).catch(function(err){
                        q.reject({code:1});
                    });
                }else{
                    q.reject({code:1});
                }
            });
            return q.promise;
        },

    }
}]);